
export default ({
  state: {
    // list: [{
    //   id: 100,
    //   name: "吃饭",
    //   isDone: true
    // },
    // {
    //   id: 201,
    //   name: "睡觉",
    //   isDone: false
    // },
    // {
    //   id: 103,
    //   name: "打豆豆",
    //   isDone: true
    // },
    // ],

    list: JSON.parse(localStorage.getItem('todos')) || [],
    type: 'all',//'all'：全部 ，'No'：未完成，'Done':'已完成'
  },
  mutations: {
    //切换分类
    changeType(state, type) {
      state.type = type
    },
    //切换元素状态
    change(state, id) {

      state.list.find(item => {
        if (item.id === id) {
          item.isDone = !item.isDone
        }
      })
    },
    //删除元素
    del(state, id) {
      state.list = state.list.filter(item => item.id != id)
    },


    //全选切换
    Allchange(state, { newList, val }) {
      newList = newList.filter(item => item.isDone = val)
    },
    //添加元素
    addItem(state, name) {
      state.list.push({
        id: +new Date(),
        name,
        isDone: false
      })
    },

    //清除已完成
    delDone(state) {
      state.list = state.list.filter(item => !item.isDone)
    }
  },
  actions: {
    delDoneAsync(context) {
      setTimeout(() => {
        context.commit('delDone')
      }, 500)
    }
  },
  getters: {
    newList(state) {
      if (state.type === 'all') {
        return state.list
      }
      else if (state.type === 'No') {
        return state.list.filter(item => !item.isDone)
      }
      else if (state.type === 'Done') {
        return state.list.filter(item => item.isDone)
      }
    }
  },
  namespaced: true
})
